package com.dfd.mango.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CallHelloController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("/call")
    public String call() {
        ServiceInstance instance = loadBalancerClient.choose("mango-producer");
        System.out.println("服务名称：" + instance.getInstanceId());
        System.out.println("服务地址：" + instance.getUri());
        String s = new RestTemplate().getForObject(instance.getUri().toString() + "/hello", String.class);
        return s;
    }
}
